use iced::{Element, Length, Padding};
use iced::widget::{text_input, TextInput, text, column, container};
use crate::theme::CdbTheme;
use super::{spacing, radius, typography};

/// Input variants
#[derive(Debug, Clone, Copy)]
pub enum InputVariant {
    Default,
    Filled,
    Outlined,
}

/// Input sizes
#[derive(Debug, Clone, Copy)]
pub enum InputSize {
    Small,
    Medium,
    Large,
}

/// Create a modern text input with Shadcn UI styling
pub fn modern_text_input<'a, Message: Clone + 'a>(
    placeholder: &str,
    value: &str,
    variant: InputVariant,
    size: InputSize,
    theme: &'a CdbTheme,
) -> TextInput<'a, Message> {
    let padding = match size {
        InputSize::Small => Padding::from([spacing::SM, spacing::MD]),
        InputSize::Medium => Padding::from([spacing::MD, spacing::LG]),
        InputSize::Large => Padding::from([spacing::LG, spacing::XL]),
    };

    text_input(placeholder, value)
        .style(move |_theme, status| {
            let colors = &theme.colors;

            match variant {
                InputVariant::Default => default_input_style(colors, status),
                InputVariant::Filled => filled_input_style(colors, status),
                InputVariant::Outlined => outlined_input_style(colors, status),
            }
        })
        .padding(padding)
        .size(match size {
            InputSize::Small => typography::SM,
            InputSize::Medium => typography::BASE,
            InputSize::Large => typography::LG,
        })
}

fn default_input_style(colors: &crate::theme::ShadcnColors, status: text_input::Status) -> text_input::Style {
    match status {
        text_input::Status::Active => text_input::Style {
            background: colors.input.into(),
            border: iced::Border {
                color: colors.border,
                width: 1.0,
                radius: radius::MD.into(),
            },
            icon: colors.muted_foreground,
            placeholder: colors.muted_foreground,
            value: colors.foreground,
            selection: colors.primary,
        },
        text_input::Status::Hovered => text_input::Style {
            background: colors.input.into(),
            border: iced::Border {
                color: colors.ring,
                width: 1.0,
                radius: radius::MD.into(),
            },
            icon: colors.muted_foreground,
            placeholder: colors.muted_foreground,
            value: colors.foreground,
            selection: colors.primary,
        },
        text_input::Status::Focused => text_input::Style {
            background: colors.input.into(),
            border: iced::Border {
                color: colors.ring,
                width: 2.0,
                radius: radius::MD.into(),
            },
            icon: colors.foreground,
            placeholder: colors.muted_foreground,
            value: colors.foreground,
            selection: colors.primary,
        },
        text_input::Status::Disabled => text_input::Style {
            background: iced::Color {
                a: 0.5,
                ..colors.muted
            }.into(),
            border: iced::Border {
                color: iced::Color {
                    a: 0.5,
                    ..colors.border
                },
                width: 1.0,
                radius: radius::MD.into(),
            },
            icon: iced::Color {
                a: 0.5,
                ..colors.muted_foreground
            },
            placeholder: iced::Color {
                a: 0.5,
                ..colors.muted_foreground
            },
            value: iced::Color {
                a: 0.5,
                ..colors.foreground
            },
            selection: colors.primary,
        },
    }
}

fn filled_input_style(colors: &crate::theme::ShadcnColors, status: text_input::Status) -> text_input::Style {
    match status {
        text_input::Status::Active => text_input::Style {
            background: colors.muted.into(),
            border: iced::Border {
                color: iced::Color::TRANSPARENT,
                width: 0.0,
                radius: radius::MD.into(),
            },
            icon: colors.muted_foreground,
            placeholder: colors.muted_foreground,
            value: colors.foreground,
            selection: colors.primary,
        },
        text_input::Status::Hovered => text_input::Style {
            background: iced::Color {
                a: 0.8,
                ..colors.muted
            }.into(),
            border: iced::Border {
                color: iced::Color::TRANSPARENT,
                width: 0.0,
                radius: radius::MD.into(),
            },
            icon: colors.muted_foreground,
            placeholder: colors.muted_foreground,
            value: colors.foreground,
            selection: colors.primary,
        },
        text_input::Status::Focused => text_input::Style {
            background: colors.muted.into(),
            border: iced::Border {
                color: colors.ring,
                width: 2.0,
                radius: radius::MD.into(),
            },
            icon: colors.foreground,
            placeholder: colors.muted_foreground,
            value: colors.foreground,
            selection: colors.primary,
        },
        text_input::Status::Disabled => text_input::Style {
            background: iced::Color {
                a: 0.5,
                ..colors.muted
            }.into(),
            border: iced::Border {
                color: iced::Color::TRANSPARENT,
                width: 0.0,
                radius: radius::MD.into(),
            },
            icon: iced::Color {
                a: 0.5,
                ..colors.muted_foreground
            },
            placeholder: iced::Color {
                a: 0.5,
                ..colors.muted_foreground
            },
            value: iced::Color {
                a: 0.5,
                ..colors.foreground
            },
            selection: colors.primary,
        },
    }
}

fn outlined_input_style(colors: &crate::theme::ShadcnColors, status: text_input::Status) -> text_input::Style {
    match status {
        text_input::Status::Active => text_input::Style {
            background: colors.background.into(),
            border: iced::Border {
                color: colors.border,
                width: 2.0,
                radius: radius::MD.into(),
            },
            icon: colors.muted_foreground,
            placeholder: colors.muted_foreground,
            value: colors.foreground,
            selection: colors.primary,
        },
        text_input::Status::Hovered => text_input::Style {
            background: colors.background.into(),
            border: iced::Border {
                color: colors.ring,
                width: 2.0,
                radius: radius::MD.into(),
            },
            icon: colors.muted_foreground,
            placeholder: colors.muted_foreground,
            value: colors.foreground,
            selection: colors.primary,
        },
        text_input::Status::Focused => text_input::Style {
            background: colors.background.into(),
            border: iced::Border {
                color: colors.ring,
                width: 3.0,
                radius: radius::MD.into(),
            },
            icon: colors.foreground,
            placeholder: colors.muted_foreground,
            value: colors.foreground,
            selection: colors.primary,
        },
        text_input::Status::Disabled => text_input::Style {
            background: iced::Color {
                a: 0.5,
                ..colors.background
            }.into(),
            border: iced::Border {
                color: iced::Color {
                    a: 0.5,
                    ..colors.border
                },
                width: 2.0,
                radius: radius::MD.into(),
            },
            icon: iced::Color {
                a: 0.5,
                ..colors.muted_foreground
            },
            placeholder: iced::Color {
                a: 0.5,
                ..colors.muted_foreground
            },
            value: iced::Color {
                a: 0.5,
                ..colors.foreground
            },
            selection: colors.primary,
        },
    }
}
